﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Entity;
using System.Web;
using HdipCricketClub.Models;
using HdipCricketClub.ViewModels;
using System.Web.Mvc;

namespace HdipCricketClub.Models
{
    public class ScorecardHeaderRepository
    {
        CricketClubEntities db = new CricketClubEntities();

        public ScorecardHeader CreateNewScorecardHeader(Fixture fixture)
        {
            ScorecardHeader scorecardHeader = new ScorecardHeader() { fixture_id = fixture.fixture_id };

            return scorecardHeader;
        }

        public ScorecardHeader GetScorecardHeader(int fixtureId)
        {
            return db.ScorecardHeaders.Include("ResultType").Where(sh => sh.fixture_id == fixtureId).Single();
        }
        
        public ScorecardHeader GetScorecardHeader(Fixture fixture)
        {
            return db.ScorecardHeaders.Where(sh => sh.fixture_id == fixture.fixture_id).Single();
        }

        public void ScorecardEntered(int fixtureId)
        {
            ScorecardHeader scorecardHeader = new ScorecardHeader { fixture_id = fixtureId,
                                                                    result_id = 1,
                                                                    won_toss = true,
                                                                    batted_first = true,
                                                                    scorecard_entered = true
                                                                   };

            db.ScorecardHeaders.AddObject(scorecardHeader);
            db.SaveChanges();
        }

        public ScorecardHeader ParseFormCollection(FormCollection formCollection)
        {
            string[] fixtureId = formCollection.GetValues("ScorecardHeaderDetail.fixture_id");
            string[] resultId = formCollection.GetValues("ScorecardHeaderDetail.result_id");
            string[] scorecardEntered = formCollection.GetValues("ScorecardHeaderDetail.scorecard_entered");
            string[] wonToss = formCollection.GetValues("ScorecardHeaderDetail.won_toss");
            string[] battedFirst = formCollection.GetValues("ScorecardHeaderDetail.batted_first");
            string[] batted = formCollection.GetValues("ScorecardHeaderDetail.batted");
            string[] bowled = formCollection.GetValues("ScorecardHeaderDetail.bowled");

            ScorecardHeader scorecardHeader = new ScorecardHeader();

            if (fixtureId[0] != "")
            {
                scorecardHeader.fixture_id = Int32.Parse(fixtureId[0]);
            }
            if (resultId[0] != "")
            {
                scorecardHeader.result_id = Int32.Parse(resultId[0]);
            }
            if (scorecardEntered[0] != "")
            {
                scorecardHeader.scorecard_entered = bool.Parse(scorecardEntered[0]);
            }
            if (wonToss[0] != "")
            {
                scorecardHeader.won_toss = bool.Parse(wonToss[0]);
            }
            if (battedFirst[0] != "")
            {
                scorecardHeader.batted_first = bool.Parse(battedFirst[0]);
            }
            if (batted[0] != "")
            {
                scorecardHeader.batted = bool.Parse(batted[0]);
            }
            if (bowled[0] != "")
            {
                scorecardHeader.bowled = bool.Parse(bowled[0]);
            }

            return scorecardHeader;
        }

        public ScorecardHeader ParseScorecardFormCollection(FormCollection formCollection)
        {
            string[] formCollectionFixtureId = formCollection.GetValues("ScorecardFixture.fixture_id");
            string[] formCollectionResultId = formCollection.GetValues("ScorecardHeaderDetail.result_id");
            string[] formCollectionScorecardEntered = formCollection.GetValues("ScorecardHeaderDetail.scorecard_entered");
            string[] formCollectionWonToss = formCollection.GetValues("ScorecardHeaderDetail.won_toss");
            string[] formCollectionBattedFirst = formCollection.GetValues("ScorecardHeaderDetail.batted_first");
            string[] formCollectionBatted = formCollection.GetValues("ScorecardHeaderDetail.batted");
            string[] formCollectionBowled = formCollection.GetValues("ScorecardHeaderDetail.bowled");
                 

            ScorecardHeader scorecardHeader = new ScorecardHeader();

            if (formCollectionFixtureId[0] != "")
            {
                scorecardHeader.fixture_id = Int32.Parse(formCollectionFixtureId[0]);
            }
            if (formCollectionResultId[0] != "")
            {
                scorecardHeader.result_id = Int32.Parse(formCollectionResultId[0]);
            }
            if (formCollectionScorecardEntered[0] != "")
            {
                scorecardHeader.scorecard_entered = bool.Parse(formCollectionScorecardEntered[0]);
            }
            if (formCollectionWonToss[0] != "")
            {
                scorecardHeader.won_toss = bool.Parse(formCollectionWonToss[0]);
            }
            if (formCollectionBattedFirst[0] != "")
            {
                scorecardHeader.batted_first = bool.Parse(formCollectionBattedFirst[0]);
            }
            if (formCollectionBatted[0] != "")
            {
                scorecardHeader.batted = bool.Parse(formCollectionBatted[0]);
            }
            if (formCollectionBowled[0] != "")
            {
                scorecardHeader.bowled = bool.Parse(formCollectionBowled[0]);
            }

            return scorecardHeader;
        }
    }



}